package com.example.c3p0.servlet; /**
 * @auther 尚智江
 * @Date 2022/5/15 15:26
 */

import com.example.c3p0.dao.UserDao;
import com.example.c3p0.dao.UserDaoImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求编码，相应方式和编码方式
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        // 获取注册页面提交的数据
        response.setContentType("text/html;charset=utf-8");
        String noteUsername = request.getParameter("username");
        String notePassword = request.getParameter("password");
        UserDao userDao = new UserDaoImpl();
        try {
            PrintWriter out = response.getWriter();
            if(userDao.login(noteUsername,notePassword)){
                out.write("登录成功");
            }else {
                out.write("登录失败");
            }
        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }
    }
}
